package com.hspedu.newData.dmsxl.数组.二分查找;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-22 
 * @Version: 1.0
 */

public class LC35搜索插入的位置 {

    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while(left <= right){
            int mid = left + (right - left) / 2;

            if (nums[mid] > target){
                right = mid - 1;
            }else if (nums[mid] < target){
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return left;
    }

    @Test
    public void test(){
        int[] nums = new int[]{1,3,5,6};

        int i = searchInsert(nums,5);
        int j = searchInsert(nums,2);
        int k = searchInsert(nums,7);
        System.out.println("==========");
    }
}
